package com.atguigu.juc.study.chapter05.interrupt;

import java.util.concurrent.TimeUnit;

/**
 * @author : leimingchao
 * @date Date : 2022年06月12日 17:28
 */
public class InterruptDemo5 {

    public static void main(String[] args) {

        Thread t1 = new Thread(() -> {

            while (true) {
                if (Thread.currentThread().isInterrupted()) {
                    System.out.println(Thread.currentThread().getName() + "\t 中断标志位：" + Thread.currentThread().isInterrupted() + " 程序停止");
                    break;
                }

                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    //为什么要在异常处再调用一次？因为对处于sleep，join，wait中的线程，其他线程调用interrupt，睡眠中打断，sleep,wait,join被打断后，中断状态被清除
                    Thread.currentThread().interrupt();
                    e.printStackTrace();
                }

                System.out.println("----hello InterruptDemo5");
            }

        }, "t1");
        t1.start();

        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        new Thread(t1::interrupt, "t2").start();

    }
}
